package com.wimcorp.magic.abilities;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;

import com.wimcorp.magic.abilities.trigger.AbilityTrigger;
import com.wimcorp.magic.board.CardAccessor;
import com.wimcorp.magic.cards.PlayingCard;
import com.wimcorp.magic.exception.CastException;
import com.wimcorp.magic.input.Condition;
import com.wimcorp.magic.input.Location;
import com.wimcorp.magic.input.Target;

/**
 * example {target:
 * 'OPPONENT',locations:['BATTLEFIELD'],behaviors:['DEATH_TOUCH'],destination
 * ['GRAVEYARD']}
 * 
 * target options : OPPONENT,SELF,THIS
 * 
 * types options : 'CREATURE','PERMANENT','ARTIFACT',LAND'
 * 
 * location options : HAND,GRAVEYARD,EXILE,DECK
 * 
 * destination : HAND,GRAVEYARD,EXILE,TOP_DECK,BOTTOM_DECK
 */
@Entity
@Table(name = "return", schema = "abilities")
public class Return extends Ability {

	@Enumerated(EnumType.STRING)
	private Location destination;

	// @Enumerated(EnumType.STRING)
	// private Condition condition;

	protected Return() {
	}

	public Return(AbilityTrigger trigger) {
		super(trigger);
	}

	public Return(Return ability) {
		super(ability);
		this.destination = ability.getDestination();
		// this.condition = ability.getCondition();
	}

	@Override
	protected void executeAbility() throws CastException {
		CardAccessor cardAccessor = null;

		for (String location : getLocations()) {
			if (cardAccessor == null) {
				cardAccessor = getCardAccesor(getInput(), location);
			}
		}
		if (cardAccessor != null) {

			PlayingCard card = cardAccessor.getCard(getInput());
			cardAccessor.remove(card);
			switch (destination) {
			case BOTTOM_DECK:
				card.getPlayer().getDeck().addOnBottom(card);
				break;
			case EXILE:
				card.exile();
				break;
			case GRAVEYARD:
				card.graveyard();
				break;
			case HAND:
				card.getPlayer().getHand().add(card);
				break;
			case TOP_DECK:
				getPlayer().getDeck().addOnTop(card);
				break;
			}
		}
	}

	@Override
	public boolean isInputRequired() {
		if (Target.THIS.equals(getTarget())) {
			return false;
		}
		return true;
	}

	public Location getDestination() {
		return destination;
	}

	/*
	 * public Condition getCondition() { return condition; }
	 */

	@Override
	public Return clone() {
		return new Return(this);
	}

	public void setDestination(Location destination) {
		this.destination = destination;
	}

	/*
	 * public void setCondition(Condition condition) { this.condition =
	 * condition; }
	 */

}
